package com.promise.jokerdream.ui.toolbar

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.border
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.zIndex
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.widget.Toast
import com.promise.jokerdream.R
import com.promise.jokerdream.ui.components.PMDialog
import com.promise.jokerdream.ui.theme.AppFontSizes

@Composable
fun ChromeStyleToolbar(
    currentUrl: String,
    onRefreshClick: () -> Unit,
    onMenuClick: () -> Unit,
    onSearchClick: () -> Unit = {},
    onUrlChange: (String) -> Unit = {},
    progress: Float = 0f
) {
    val context = LocalContext.current
    var showUrlDialog by remember { mutableStateOf(false) }
    Surface(
        modifier = Modifier
            .fillMaxWidth()
            .zIndex(1f),
        color = Color.White,
        shadowElevation = 2.dp
    ) {
        Column {
            // 状态栏安全区域
            Spacer(
                modifier = Modifier
                    .fillMaxWidth()
                    .windowInsetsPadding(WindowInsets.statusBars)
                    .height(0.dp)
            )
            
            // 主工具栏
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(48.dp)
                    .padding(horizontal = 8.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                // 菜单按钮
                IconButton(
                    onClick = onMenuClick,
                    modifier = Modifier.size(40.dp)
                ) {
                    Icon(
                        imageVector = Icons.Default.Menu,
                        contentDescription = "菜单",
                        modifier = Modifier.size(20.dp)
                    )
                }
                
                // URL显示区域（包含复制和刷新按钮）
                Surface(
                    modifier = Modifier
                        .weight(1f)
                        .height(40.dp)
                        .padding(horizontal = 8.dp)
                        .border(1.dp, Color.Black, RoundedCornerShape(16.dp)),
                    shape = RoundedCornerShape(16.dp),
                    color = Color.White
                ) {
                    Row(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(horizontal = 4.dp),
                        verticalAlignment = Alignment.CenterVertically
                    ) {
                        // URL文本（可点击打开对话框）
                        Text(
                            text = currentUrl,
                            fontSize = AppFontSizes.mediumBody,
                            maxLines = 1,
                            overflow = TextOverflow.Ellipsis,
                            modifier = Modifier
                                .weight(1f)
                                .clickable { showUrlDialog = true }
                                .padding(horizontal = 8.dp)
                        )
                        
                        // 复制按钮
                        IconButton(
                            onClick = {
                                val clipboard = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
                                val clip = ClipData.newPlainText("URL", currentUrl)
                                clipboard.setPrimaryClip(clip)
                                Toast.makeText(context, "已复制", Toast.LENGTH_SHORT).show()
                            },
                            modifier = Modifier.size(32.dp)
                        ) {
                            Icon(
                                painter = painterResource(id = R.drawable.icon_copy),
                                contentDescription = "复制",
                                modifier = Modifier.size(20.dp),
                                tint = MaterialTheme.colorScheme.onSurfaceVariant
                            )
                        }
                        
                        // 刷新按钮
                        IconButton(
                            onClick = onRefreshClick,
                            modifier = Modifier.size(32.dp)
                        ) {
                            Icon(
                                painter = painterResource(id = R.drawable.icon_refresh),
                                contentDescription = "刷新",
                                modifier = Modifier.size(20.dp),
                                tint = MaterialTheme.colorScheme.onSurfaceVariant
                            )
                        }
                    }
                }
                
                // 搜索按钮
                IconButton(
                    onClick = onSearchClick,
                    modifier = Modifier.size(40.dp)
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.icon_search),
                        contentDescription = "搜索",
                        modifier = Modifier.size(20.dp)
                    )
                }
            }
            
            // 进度条 - 始终占用空间避免跳动
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(2.dp)
            ) {
                if (progress > 0f && progress < 1f) {
                    LinearProgressIndicator(
                        progress = { progress },
                        modifier = Modifier
                            .fillMaxWidth()
                            .height(2.dp),
                        color = MaterialTheme.colorScheme.primary,
                        trackColor = MaterialTheme.colorScheme.surfaceVariant
                    )
                }
            }
        }
    }
    
    // URL输入对话框
    if (showUrlDialog) {
        var urlInput by remember { mutableStateOf(currentUrl) }
        
        // 每次显示对话框时更新输入值
        LaunchedEffect(showUrlDialog) {
            if (showUrlDialog) {
                urlInput = currentUrl
            }
        }
        
        PMDialog(
            show = showUrlDialog,
            title = "网页地址",
            confirmText = "前往",
            cancelText = "取消",
            onDismiss = { showUrlDialog = false },
            onConfirm = {
                if (urlInput.isNotEmpty()) {
                    onUrlChange(urlInput.trim())
                    showUrlDialog = false
                }
            }
        ) {
            OutlinedTextField(
                value = urlInput,
                onValueChange = { urlInput = it },
                label = { Text("URL") },
                placeholder = { Text("请输入网址...") },
                maxLines = 5,
                modifier = Modifier.fillMaxWidth()
            )
        }
    }
}
